<script setup>
	import {
		onLaunch
	} from '@dcloudio/uni-app';
	import {
		ref,
		onMounted
	} from "vue";
	import {
		Mqtt
	} from "./mqtt.js"
	import {
		Ble
	} from "./ble.js";
	import {
		wifiResultApi
	} from "@/api/request.js";
	import { CacheUtils } from '@/utils/index.js';
	import { useI18n } from 'vue-i18n';
		
	const { t } = useI18n();
	const ble = ref(null);
	const mqtt = ref(null);

	const initService = () => {
		mqtt.value = new Mqtt()
		ble.value = new Ble();
		mqtt.value.init();
	}

	const listenEvents = () => {
		uni.$on('weishi', (params) => {
			mqtt.value.weishi(params)
		});
		uni.$on('weishishijian', (params) => {
			mqtt.value.weishishijian(params)
		});
		uni.$on('delcmd', (params) => {
			mqtt.value.delcmd(params)
		});
		// 链接
		uni.$on('lianjie', (obj) => {
			mqtt.value.init()
		});

		uni.$on('chongxinlianjie', (obj) => {
			mqtt.value.init()
		});

		uni.$on('addTopic', (obj) => {
			mqtt.value.addTopic({
				imei: obj.imei
			})
		});
		uni.$on('removeTopic', (obj) => {
			mqtt.value.removeTopic(obj.imei)
		});
		// 获取喂水器信息
		uni.$on("reqWaterInfo", () => {			
			mqtt.value.waterInfo();
		});
		// 修改喂水器模式请求
		uni.$on("reqWaterModel", (model) => {			
			mqtt.value.waterModelChange(model);
		});
		// 修改喂水器开发状态请求
		uni.$on("reqWaterOpenChange", (isOpen) => {			
			mqtt.value.waterOpenStatusChange(isOpen);
		});
		//断开
		uni.$on('duankai', () => {
			mqtt.value.client.end()
		});


		/**
		 * 蓝牙开始啦
		 */
		uni.$on("blelianjie", (id, name) => {

			ble.value.connectToDevice(id, name);
		});
		uni.$on("blesendcmd", (mac, cmd) => {
			ble.value.sendCommand(ble.value.ble.id, cmd);
		});
		uni.$on("bleduankai", () => {
			ble.value.disconnectBluetooth();
		});
		//是否配网
		uni.$on("getNetWork", () => {
			mqtt.value.sendMessage({
				cmd: "queryconfig",
			});
		});		

		uni.$on('wifiJieguo', async () => {
			let res = await wifiResultApi({
				mac: ble.value.ble.devicemac,
				userid: CacheUtils.get('userid'),
			})

			if (res.response.result == true) {
				uni.showToast({
					title: t('common.operate_ok'),
					icon: 'none',
					duration: 1200
				})
				//配置网络成功下一步
				uni.$emit('wifiResult')
			} else {
				uni.showModal({
					title: t('pages.addDevice.addDeivceFail_title'),
					showCancel: false,
					content:  t('pages.addDevice.addDeivceFail_desc'),
					success: function(res) {
						if (res.confirm) {
							// console.log('用户点击确定');
						} else if (res.cancel) {
							// console.log('用户点击取消');
						}
					}
				});
			}
		})
	}

	const addDeviceInstantly = () => {
		//添加设备
		uni.$emit('addSHebei', async () => {
			let res = await wifiResultApi({
				mac: ble.value.uuid,
				userid: CacheUtils.get('userid'),
			})
			if (res.response.result == true) {
				uni.showToast({
					title: res.response.msg,
					icon: 'none',
					duration: 1200
				})
				//配置网络成功下一步
				uni.$on('wifiResult')
			} else {
				uni.showToast({
					title: res.response.msg,
					icon: 'none',
					duration: 1200
				})

			}
		})
	}
	
	// onMounted(() => {
	// 	initService();
	// 	listenEvents();
	// 	addDeviceInstantly();
	// })

	onLaunch(() => {
		initService();
		listenEvents();
		addDeviceInstantly();
		// uni.setLocale(import.meta.env.VITE_DEFAULT_LOCALE);
	})
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "@/uni_modules/uview-plus/index.scss";
	@import url(./styles/index.scss);

	/** cell样式的菜单列表 **/
	.menu-list {
		display: block;
		margin: 0 auto;
		width: 650rpx;
		box-sizing: border-box;
		padding: 10rpx 30rpx;
		border-radius: 20rpx;
		background-color: #FFFFFF;
		color: #1F2937;

		.menu-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 80rpx;
			font-size: 30rpx;

			.label-box {
				display: inline-flex;
				align-items: center;
				column-gap: 8rpx;
				flex-direction: row;
			}

			.icon {
				display: inline-flex !important;
			}

			&:not(:first-child) {
				border-top: 2px dotted #F8F7F2;
			}
		}
	}

    .menu-seprate-list {
		display: block;
		margin: 0 auto;
		width: 650rpx;
		color: #1F2937;

		.menu-item {
            background-color: #FFFFFF;
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 60rpx;
			font-size: 30rpx;
            margin-bottom: 30rpx;
		    padding: 20rpx 30rpx;
		    border-radius: 24rpx;
            box-shadow: 0px 2rpx 4rpx 0px rgba(0,0,0,0.05);

			.label-box {
				display: inline-flex;
				align-items: center;
				column-gap: 8rpx;
				flex-direction: row;
			}

			.icon {
				display: inline-flex !important;
			}

			&:not(:first-child) {
				border-top: 2px dotted #F8F7F2;
			}
		}
	}
</style>